home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / FOPEN.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  83 lines

  1. /* This is file FOPEN.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1980 Regents of the University of California.
  9.  * All rights reserved.  The Berkeley software License Agreement
  10.  * specifies the terms and conditions for redistribution.
  11.  */
  12.  
  13. #if defined(LIBC_SCCS) && !defined(lint)
  14. static char sccsid[] = "@(#)fopen.c    5.2 (Berkeley) 3/9/86";
  15. #endif LIBC_SCCS and not lint
  16.  
  17. #include <sys/types.h>
  18. #include <sys/file.h>
  19. #include <stdio.h>
  20.  
  21. FILE *
  22. fopen(file, mode)
  23.     const char *file;
  24.     register const char *mode;
  25. {
  26.     register FILE *iop;
  27.     register f, rw, oflags;
  28.     extern FILE *_findiop();
  29.     char tbchar;
  30.  
  31.     iop = _findiop();
  32.     if (iop == NULL)
  33.         return (NULL);
  34.  
  35.     rw = (mode[1] == '+');
  36.  
  37.     switch (*mode) {
  38.     case 'a':
  39.         oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
  40.         break;
  41.     case 'r':
  42.         oflags = rw ? O_RDWR : O_RDONLY;
  43.         break;
  44.     case 'w':
  45.         oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
  46.         break;
  47.     default:
  48.         return (NULL);
  49.     }
  50.     if (mode[1] == '+')
  51.         tbchar = mode[2];
  52.     else
  53.         tbchar = mode[1];
  54.     if (tbchar == 't')
  55.         oflags |= O_TEXT;
  56.     else if (tbchar == 'b')
  57.         oflags |= O_BINARY;
  58.     else
  59.         oflags |= (_fmode & (O_TEXT|O_BINARY));
  60.  
  61.     f = open(file, oflags, 0666);
  62.     if (f < 0)
  63.         return (NULL);
  64.  
  65.     if (*mode == 'a')
  66.         lseek(f, (off_t)0, L_XTND);
  67.  
  68.     iop->_cnt = 0;
  69.     iop->_file = f;
  70.     iop->_bufsiz = 0;
  71.     if (rw)
  72.         iop->_flag = _IORW;
  73.     else if (*mode == 'r')
  74.         iop->_flag = _IOREAD;
  75.     else
  76.         iop->_flag = _IOWRT;
  77.     if (oflags & O_TEXT)
  78.         iop->_flag |= _IOTEXT;
  79.  
  80.     iop->_base = iop->_ptr = NULL;
  81.     return (iop);
  82. }
  83.